//
//  MineInfoViewController.m
//  CharlesMVX
//
//  Created by Charles on 2020/3/11.
//  Copyright © 2020 Charles. All rights reserved.
//

#import "MVCInfoViewController.h"
#import "MVCUserViewController+Protect.h"
#import "UserInfoView.h"
#import "Masonry.h"

@interface MVCInfoViewController ()

@property (nonatomic, strong) UIButton *blogButton;
@property (nonatomic, strong) UIButton *draftButton;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MVCDraftController *draftVC;

@end

@implementation MVCInfoViewController

- (void)switchTableView:(UIButton *)button {
    if (button == self.blogButton) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
        self.draftButton.selected = NO;
    }else {
        self.blogButton.selected = NO;
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }
    button.selected = YES;
}

- (void)configuration {
    [super configuration];
    self.title = @"我";
    self.draftVC = [MVCDraftController instanceWithUserId:self.userId];
    
    __weak typeof (self) weakSelf = self;
    [self.draftVC setDidSelectedRowHandler:^(Draft * draft) {
        [weakSelf.navigationController pushViewController:[[DetailViewController alloc]init] animated:YES];
    }];
}

- (void)addUI {
    [super addUI];
    UIButton *(^makeButton)(NSString *, CGRect) = ^(NSString *title, CGRect frame) {
        
        UIButton *button = [[UIButton alloc] initWithFrame:frame];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(switchTableView:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    };
    
    
    CGFloat switchButtonTop = 260 + NAVBAR_HEIGHT;
    CGFloat switchButtonHeight = 60;
    [self.view addSubview:self.blogButton = makeButton(@"博客", CGRectMake(0, switchButtonTop, SCREEN_WIDTH * 0.5, switchButtonHeight))];
    [self.view addSubview:self.draftButton = makeButton(@"草稿", CGRectMake(SCREEN_WIDTH * 0.5, switchButtonTop, SCREEN_WIDTH * 0.5, switchButtonHeight))];
    self.blogButton.selected = YES;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.blogButton.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.blogButton.frame))];
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    scrollView.scrollEnabled = NO;
    [self.view addSubview:self.scrollView = scrollView];
    
    [self.blogListController.tableView removeFromSuperview];
    [self.blogListController.tableView reloadData];

    [self.scrollView addSubview:self.blogListController.tableView];
    [self.scrollView addSubview:self.draftVC.tableView];
    
    [self.blogListController.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.inset(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(self.scrollView.mas_height);
    }];
    
    [self.draftVC.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.inset(0);
        make.left.equalTo(self.blogListController.tableView.mas_right);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(self.scrollView.mas_height);
    }];
}

- (void)fetchData {
    [super fetchData];
    
    [self.draftVC fetchData];
}

@end
